package talk.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.talk.debby.talk.R;

import java.util.HashMap;
import java.util.Map;

import base.BaseActivity;
import talk.Bean.Blank;
import talk.Bean.Choice;
import talk.Bean.MyUser;
import talk.Data.Constant;
import talk.Utils.ChoiceUtil;
import talk.listener.BlankListener;
import talk.listener.ChoiceListener;


/**
 * Created by debby on 2018/3/14.
 * 描述:模板
 */

public class QuestionActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout ll_content;
    private int currentPosition = 0;
    private SparseIntArray scores = new SparseIntArray();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aacrivity_question);
        initView();
        initData();
    }

    private void initView() {
        ll_content = findViewById(R.id.ll_content);

        findViewById(R.id.iv_left).setOnClickListener(this);
        findViewById(R.id.iv_right).setOnClickListener(this);
    }


    private void initData() {
        changeQuestion(1);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_left:
                changeQuestion(currentPosition - 1);
                break;
            case R.id.iv_right:
                changeQuestion(currentPosition + 1);
                break;
        }
    }

    private void changeQuestion(int position) {
        if (position > 7 || position < 1) return;
        currentPosition = position;
        switch (position) {
            case 1:
                changeChoice(ChoiceUtil.getChoice1());
                break;
            case 2:
                changeChoice(ChoiceUtil.getChoice2());
                break;
            case 3:
                changeChoice(ChoiceUtil.getChoice3());
                break;
            case 4:
                changeChoice(ChoiceUtil.getChoice4());
                break;
            case 5:
                changeBlank(ChoiceUtil.addBlank1());
                break;
            case 6:
                changeBlank(ChoiceUtil.addBlank2());
                break;
//            case 7:
//                addScore();
//                break;
        }
    }

    private void changeChoice(final Choice choice) {
        View view = ChoiceUtil.addView(this, choice, new ChoiceListener() {
            @Override
            public void onItemClick(int position) {
                if (choice.getOptionList().get(position).isTrue) {
                    showToast("回答正确，得" + choice.getValue() + "分");
                    scores.put(currentPosition, choice.getValue());
                } else {
                    showToast("回答错误,正确答案为" + choice.getAnswer());
                    scores.put(currentPosition, 0);
                }
            }
        });
        ll_content.removeAllViews();
        ll_content.addView(view);
    }

    private void changeBlank(final Blank blank) {
        View view = ChoiceUtil.addView1(this, blank, new BlankListener() {
            @Override
            public void confirm(String answer) {
                if (TextUtils.isEmpty(answer)) {
                    showToast("请输入结果");
                    return;
                }
                if (answer.equals(blank.getAnswer())) {
                    showToast("回答正确，得" + blank.getValue() + "分");
                    scores.put(currentPosition, blank.getValue());
                } else {
                    showToast("回答错误,正确答案为" + blank.getAnswer());
                    scores.put(currentPosition, 0);
                }
            }
        });
        ll_content.removeAllViews();
        ll_content.addView(view);
    }

    private void addScore() {
        View view = LayoutInflater.from(this).inflate(
                R.layout.view_score, null);
        MyUser user = Constant.user;
        SimpleDraweeView simpleDraweeView = view.findViewById(R.id.sd_portrait);
        TextView tv_user = view.findViewById(R.id.tv_user);
        if (user != null) {
            simpleDraweeView.setImageURI(user.getSinfo().getImgurl());
            tv_user.setText(user.getSinfo().getUname());
        }


        TextView tv_score = view.findViewById(R.id.tv_score);
        tv_score.setText(getScore() + "分");
        view.findViewById(R.id.bt_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        ll_content.removeAllViews();
        ll_content.addView(view);
    }

    private int getScore() {
        int score = 0;
        for (int i = 0; i < scores.size(); i++) {
            score = scores.get(i);
        }
        return score;
    }
}
